package com.baoteng.ui.toast;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

import java.lang.ref.WeakReference;

public class BaseToast {
    private static final String Tag = "BaseToast";
    private static WeakReference<Toast> sToastRef;

    private BaseToast() throws Exception {
        throw new Exception();
    }

    public static void showToast(Context context, @StringRes int resId) {
        if (context == null) {
            return;
        }
        String msg = context.getResources().getString(resId);
        showToast(context, msg, false);
    }

    public static void showToast(Context context, @StringRes int resId, boolean lengthLong) {
        if (context == null) {
            return;
        }
        context = context.getApplicationContext();
        String msg = context.getResources().getString(resId);
        showToast(context, msg, lengthLong);
    }

    public static void showToast(Context context, String msg) {
        if (context == null) {
            return;
        }
        context = context.getApplicationContext();
        showToast(context, msg, false);
    }

    public static void showToast(Context context, final String msg, final boolean lengthLong) {
        if (context == null) {
            return;
        }
        toastDefault(context, msg, lengthLong);
    }

    public static void showToast(Context context, String msg, Drawable leftIcon) {
        if (context == null) {
            return;
        }
        showToastInternal(context, msg, ToastLevel.DEFAULT, false, leftIcon);
    }

    public static void toastDefault(Context context, String strMsg, boolean toastLong) {
        showToastInternal(context, strMsg, ToastLevel.DEFAULT, toastLong);
    }

    public static void toastDefault(Context context, @StringRes int strResId, boolean toastLong) {
        toastDefault(context, context.getResources().getString(strResId), toastLong);
    }

    public static void toastLoading(Context context, @StringRes int strResId, boolean toastLong) {
        toastLoading(context, context.getResources().getString(strResId), toastLong);
    }

    public static void toastLoading(Context context, String strMsg, boolean toastLong) {
        showToastInternal(context, strMsg, ToastLevel.LOADING, toastLong);
    }

    public static void toastSuccess(Context context, @StringRes int strResId, boolean toastLong) {
        toastSuccess(context, context.getResources().getString(strResId), toastLong);
    }

    public static void toastSuccess(Context context, String strMsg, boolean toastLong) {
        showToastInternal(context, strMsg, ToastLevel.SUCCESS, toastLong);
    }

    public static void toastSuccess(Context context, String strMsg) {
        showToastInternal(context, strMsg, ToastLevel.SUCCESS, false);
    }

    public static void toastSuccess(Context context, @StringRes int strResId) {
        showToastInternal(context, context.getResources().getString(strResId), ToastLevel.SUCCESS, false);
    }

    public static void toastException(Context context, @StringRes int strResId, boolean toastLong) {
        toastException(context, context.getResources().getString(strResId), toastLong);
    }

    public static void toastException(Context context, String strMsg, boolean toastLong) {
        showToastInternal(context, strMsg, ToastLevel.EXCEPTION, toastLong);
    }

    public static void toastException(Context context, @StringRes int strResId) {
        showToastInternal(context, context.getResources().getString(strResId), ToastLevel.EXCEPTION, false);
    }

    public static void toastException(Context context, String strMsg) {
        showToastInternal(context, strMsg, ToastLevel.EXCEPTION, false);
    }

    private static void showToastInternal(final Context context,
                                          final String strMsg, @ToastLevel final int toastLevel,
                                          final boolean toastLong) {
        showToastInternal(context, strMsg, toastLevel, toastLong, null);
    }

    @SuppressLint("MissingPermission")
    private static void showToastInternal(final Context context,
                                          final String msg, @ToastLevel final int toastLevel,
                                          final boolean toastLong, final Drawable leftIcon) {
        if (context == null || TextUtils.isEmpty(msg)) {
            Log.w(Tag, logWarnMsg(toastLevel, context, msg));
            return;
        }
        final String strMsg = msg;
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                cancelToast();
                Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
                toast.show();
                sToastRef = new WeakReference<>(toast);
            }
        });
    }

    public static void cancelToast() {
        WeakReference<Toast> ref = sToastRef;
        if (ref == null)
            return;
        Toast toast = ref.get();
        if (toast == null)
            return;
        toast.cancel();
        ref.clear();
    }

    private static String logWarnMsg(int toastLevel, Context context, String msg) {
        return "Toast level = " + toastLevel + " [ context = " + context + " , msg = " + msg + " ]";
    }
}
